package cat.tom.controller;

import cat.tom.pojo.User;
import cat.tom.service.UserService;
import cat.tom.utils.JwtHelper;
import cat.tom.utils.Result;
import cat.tom.utils.ResultCodeEnum;
import com.alibaba.druid.util.StringUtils;
import org.apache.el.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author shkstart
 * @create 2024-03-08 12:37
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    JwtHelper jwtHelper;

    @PostMapping("login")
    public Result login(@RequestBody User user){
        Result result =  userService.login(user);
        return result;

    }


    @GetMapping("getUserInfo")
    public Result userInfo(@RequestHeader String token){

        Result result = userService.getUserInfo(token);
        return result;

    }

    @PostMapping("checkUserName")
    public Result checkUserName(String username){

        Result result = userService.checkUserName(username);
        return result;

    }

    @PostMapping("regist")
    public Result regist(@RequestBody User user){

        Result result = userService.regist(user);
        return result;
    }

    @GetMapping("checkLogin")
    public Result checkLogin(@RequestHeader String token){

        if (StringUtils.isEmpty(token) || jwtHelper.isExpiration(token))
            return Result.build(null, ResultCodeEnum.NOTLOGIN);

        return Result.ok(null);
    }
}
